import {inject, Injectable} from '@angular/core';
import {HostService} from "./host.service";
import {HttpClient} from "@angular/common/http";
import {StorageService} from "./storage.service";
import {Dept} from "../model/auth/Dept";
import {Member} from "../model/auth/Member";
import {Rolegroup} from "../model/auth/Rolegroup";


@Injectable({
  providedIn: 'root'
})
export class AuthService {

  private host:HostService=inject(HostService);
  private http:HttpClient=inject(HttpClient);
  private storage:StorageService=inject(StorageService);

  constructor() { }

  private loginByPasswordURL=this.host.HOST+"public/loginByPass";
  loginByPassword(username:string,password:string){
    let parm={
      "username":username,
      "pass":password
    }
    return this.http.post(this.loginByPasswordURL,parm).toPromise();

  }


  private refreshTokenURL=this.host.HOST+"member/refreshToken";
  refreshToken(){
    let last:number= 0;
    let current:number=new Date().getTime();
    this.storage.get("FRESHTIME")
      .then((value:number)=>{
        // console.log("currenttime"+current);
        //  console.log("lastfreshtime"+value);
        if(current-value>(10*60*1000)){
          this.http.get(this.refreshTokenURL).toPromise()
            .then((data:any)=>{
              if(data.token!='error'){
                this.storage.set("TOKEN",data.token);

                this.storage.set("FRESHTIME",current);

                console.log("令牌成功刷新");
              }
            })
        }
      })
  }



  private saveDeptURL=this.host.HOST+"member/saveDept";
  saveDept(d:Dept){
    return this.http.post(this.saveDeptURL,d).toPromise();
  }

  private deleteDeptByIdURL=this.host.HOST+"member/deleteDeptById";
  deleteDeptById(id:string){
    let parm={
      "id":id
    }
    return this.http.post(this.deleteDeptByIdURL,parm).toPromise();
  }

  private getDeptByIdURL=this.host.HOST+"member/getDeptById";
  getDeptById(deptid:string){
    let parm={
      "id":deptid
    }
    return this.http.post(this.getDeptByIdURL,parm).toPromise();
  }

  private getDeptNzTreeURL=this.host.HOST+"member/getDeptTreeNodeList";
  getDeptNzTree(){
    return this.http.get(this.getDeptNzTreeURL).toPromise();
  }


  private getMemberListByDeptURL=this.host.HOST+"member/getMemberListByDept";
  getMemberListByDept(deptid:string){
    let parm={
      "deptid":deptid
    }
    return this.http.post(this.getMemberListByDeptURL,parm).toPromise();
  }

  private getMemberByIdURL=this.host.HOST+"member/getMemberById";
  getMemberById(id:string){
    let parm={
      id:id
    }
    return this.http.post(this.getMemberByIdURL,parm).toPromise();
  }

  private deleteMemberByidURL=this.host.HOST+"member/deleteMemberByid";
  deleteMemberByid(id:string){
    let pamr={
      id:id
    }
    return this.http.post(this.deleteMemberByidURL,pamr).toPromise();
  }

  private saveMemberURL=this.host.HOST+"member/saveMember";
  saveMember(m:Member){
    return this.http.post(this.saveMemberURL,m).toPromise();
  }


  private saveRoleGroupURL=this.host.HOST+"member/saveRoleGroup";
  saveRoleGroup(r:Rolegroup){
    return this.http.post(this.saveRoleGroupURL,r).toPromise();
  }

  private getRolegroupByIdURL=this.host.HOST+"member/getRolegroupById";
  getRolegroupById(id:string){
    let parm={
      id:id
    }
    return this.http.post(this.getRolegroupByIdURL,parm).toPromise();
  }

  private getRolegroupListURL=this.host.HOST+"member/getRolegroupList";
  getRolegroupList(){
    return this.http.get(this.getRolegroupListURL).toPromise();
  }




}
